var req = require('../../interfaceImpl.js');
var config = require('../../interfaceConfig.js');
var total_micro_second = 5 * 60 * 1000;
var strAppCode = config.SmallApp;
var youhuilist = [];
var ReqmberAccount = ""; //优惠卷签名
var app = getApp();
var conpons = '';
var finalMoney = "";
var needpay = "";
var haschooseseat = [];
var nopass = false;
var pojo = "";
var reqdetailSign = "";
var huiyuanprice ="";
var wxprice = "";
/* 毫秒级倒计时 */
function count_down(that) {
  // 渲染倒计时时钟
  that.setData({
    clock: date_format(total_micro_second)
  });

  if (total_micro_second <= 0) {
    that.setData({
      clock: "已经截止",
      issuccess: false,
    });
    // timeout则跳出递归
    return;
  }
  setTimeout(function () {
    // 放在最后--
    total_micro_second -= 10;
    count_down(that);
  }
    , 10)
}

// 时间格式化输出，如03:25:19 86。每10ms都会调用一次
function date_format(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = Math.floor(second / 3600);
  // 分钟位
  var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
  // 秒位
  var sec = fill_zero_prefix((second - hr * 3600 - min * 60));// equal to => var sec = second % 60;
  // 毫秒位，保留2位
  var micro_sec = fill_zero_prefix(Math.floor((micro_second % 1000) / 10));

  return min + "分" + sec + "秒";
}

// 位数不足补零
function fill_zero_prefix(num) {
  return num < 10 ? "0" + num : num
}
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    count_down(this);
   pojo = JSON.parse(options.pojo);


  //  var detailSign = "strAppCode=" + strAppCode + "OrderTicketID=" + pojo.OrderTicketID
  //  return req.GetSignStr(detailSign).then(res => {
  //    reqdetailSign = res.data.data
  //  }).then(()=>{
  //    return req.GetOrderDetailList(strAppCode, pojo.OrderTicketID, reqdetailSign, app.globalData.token)
  //  }).then(res=>{
  //    console.log(res)
  //  })



   
    needpay = finalMoney = pojo.PriceTotal;
    console.log(pojo)
    haschooseseat = pojo.haschooseseat
    this.setData({ pojo, needpay: needpay });
    //TODO请求 优惠卷
    var ReqmberAccountSign = "strAppCode=" + strAppCode + "OrderTicketID=" + pojo.OrderTicketID
    req.GetSignStr(ReqmberAccountSign).then(res => {
      if (res.data.success) {
        ReqmberAccount = res.data.data
      }
    }).then(() => {
      //请求优惠卷
      return req.mberAccount(strAppCode, pojo.OrderTicketID, ReqmberAccount, app.globalData.token).then(res => {
        youhuilist = res.data.data;
        var msgvalue = res.data.msgvalue.split(",")
        console.log(msgvalue)
        huiyuanprice = msgvalue[0];
        wxprice = msgvalue[1]
        console.log(youhuilist)
        if (youhuilist.length > 0) {
          that.setData({ youhuilist: youhuilist })
        }
        else {
          that.setData({ noyouhui: true })
        }
      })
    })
  },

  onShow() {
  
  },
  radioCheckedChange: function (e) {
    this.setData({
      radioCheckVal: e.detail.value
    })
  },

  choosecoupons: function (e) {
    needpay = finalMoney;
    console.log(haschooseseat)
    var ticketnum = haschooseseat.length;
    if (e.detail.value.length > ticketnum) {
      wx.showModal({
        title: '提示',
        content: '您购买了' + ticketnum + '张电影票' + '最多只能使用' + ticketnum + '张优惠卷',
      })
      nopass = true;
      return;
    } else {
      nopass = false;
      conpons = "";
      var ticketmoney = needpay / haschooseseat.length;
      needpay = needpay - ticketmoney * e.detail.value.length;
      this.setData({ needpay: needpay })
      for (var i = 0; i < e.detail.value.length; i++) {
        if (i == e.detail.value.length - 1) {
          conpons += e.detail.value[i];
        } else {
          conpons += e.detail.value[i] + ','
        }
      }

      console.log(conpons)
    }
  },
  gozhifu:function(e){
    //TODO校验优惠卷
    if (nopass) {
      wx.showModal({
        title: '提示',
        content: '优惠卷超过使用上限',
      })
      return false;
    }
    var that = this;

    console.log(huiyuanprice)
    console.log(wxprice)
    //跳转到支付成功页面
    wx.navigateTo({
      url: '../memberpay/paymethod?pojo=' + JSON.stringify(pojo) + '&&conpons=' + conpons + '&&needpay=' + needpay +'&&huiyuanprice='+huiyuanprice+'&&wxprice='+wxprice+''
    })
  }
})